package com.travel.controller;

import com.travel.common.Result;
import com.travel.domain.entity.ScenicProfile;
import com.travel.domain.entity.ScenicTypes;
import com.travel.service.ScenicProfileService;
import com.travel.service.ScenicTypesService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.concurrent.TimeUnit;

@Slf4j
@RestController
@RequestMapping("/scenicType")
public class ScenicTypeController {
    @Autowired
    private RedisTemplate<String, Object> redisTemplate;

    private static final String CACHE_KEY = "scenic:types"; // 景区类型列表的缓存键
    @Autowired
    private ScenicTypesService scenicTypesService;
    /**
     * 获取景区类型列表
     * @return 类型列表封装的结果
     */
    @GetMapping("/getTypeList")
    public Result<List<ScenicTypes>> getTypes() {
        // 尝试从缓存中获取景区类型列表
        List<ScenicTypes> cachedTypeList = (List<ScenicTypes>) redisTemplate.opsForValue().get(CACHE_KEY);

        if (cachedTypeList != null) {
            // 如果缓存中有数据，直接返回缓存数据
            return Result.success(cachedTypeList);
        }
        try {
            // 如果缓存中没有数据，则从数据库查询
            List<ScenicTypes> typeList = scenicTypesService.listAllTypes();

            // 将查询结果存入缓存，并设置过期时间（如 5 分钟）
            redisTemplate.opsForValue().set(CACHE_KEY, typeList);

            return Result.success(typeList);
        } catch (Exception e) {
            // 捕获异常并记录错误
            log.error("获取景区类型列表失败: {}", e.getMessage());
            return Result.error("获取景区类型列表失败");
        }
    }
}
